package com.wfm;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import org.junit.jupiter.api.Test;

public class FileDemo {

    // 路径相关
    // 读取资源路径的文件
    // 读取普通路径的文件
    // 写文件
    // 列出文件夹下的文件
    // 列出文件夹和文件
    // 以什么结尾的文件
    // 二进制文件比如图片

    public static void main(String[] args) {
        Path path = Paths.get("/Users/fuminwu/IdeaProjects/springBoot-study/logs/file.txt");

        System.out.println("Path: " + path);
        System.out.println("File Name: " + path.getFileName());
        System.out.println("Parent: " + path.getParent());
        System.out.println("Root: " + path.getRoot());

        // 获取路径中的元素
        for (int i = 0; i < path.getNameCount(); i++) {
            System.out.println("Element " + i + ": " + path.getName(i));
        }

        // 用于将一个相对路径字符串解析为当前路径的子路径。
        Path base = Paths.get("/Users/fuminwu/IdeaProjects/springBoot-study/logs");
        Path subPath = base.resolve("file1.txt");
        System.out.println("Resolved Path: " + subPath);

        // 移除路径中的.和..等冗余部分，返回一个规范化的路径。
        Path path1 = Paths.get("/home/user/abc/../files/file.txt");
        Path normalized = path1.normalize();
        System.out.println("Normalized Path: " + normalized);

        Path path2 = Paths.get("documents/file.txt");
        Path absolute = path2.toAbsolutePath();
        System.out.println("Absolute Path: " + absolute);

        System.out.println("user.dir: "+System.getProperty("user.dir"));

    }

    @Test
    public void filesTest(){
        Path filePath = Paths.get("file.txt");
        Path dirPath = Paths.get("exampleDir");

        try {
            // 创建文件
            if (!Files.exists(filePath)) {
                Files.createFile(filePath);
                System.out.println("File created: " + filePath);
            }

            // 创建目录
            if (!Files.exists(dirPath)) {
                Files.createDirectory(dirPath);
                System.out.println("Directory created: " + dirPath);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void filesTest2(){

        Path filePath = Paths.get("example.txt");

        try {
            List<String> lines = Files.readAllLines(filePath);
            for (String line : lines) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test1(){
        Path filePath = Paths.get("example.txt");
        List<String> lines = Arrays.asList("First line", "Second line", "Third line");

        try {
            Files.write(filePath, lines, StandardCharsets.UTF_8);
            System.out.println("File written: " + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test2(){
        Path sourcePath = Paths.get("example.txt");
        Path targetPath = Paths.get("example_copy.txt");
        try {
            Files.copy(sourcePath, targetPath);
            System.out.println("File copied: " + targetPath);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void test4() throws IOException, URISyntaxException {
        URL resource = FileDemo.class.getClassLoader().getResource("a.txt");
        System.out.println(resource);

        assert resource != null;
        Path path = Paths.get(resource.toURI());
        System.out.println(path.toAbsolutePath());

    }

}
